package com.shenzhen.citizen.ui;

import com.shenzhen.citizen.R;
import com.shenzhen.citizen.bean.ApiConstant;



import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebViewActivity extends Activity implements View.OnClickListener {
    /** Called when the activity is first created. */
	
	public static WebViewActivity webViewActivity;
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		webViewActivity = null;
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.web_view);
        webViewActivity = this;
        initView();
        getDataFromIntent();
    }
    private void getDataFromIntent() {
    	Intent intent = getIntent();
    	String url = intent.getStringExtra("url");
    	webView.loadUrl(url);
	}
	WebView webView;
	ProgressBar progressBar;
	protected void initView() {
		CookieSyncManager.createInstance(this); 
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.setAcceptCookie(false);
		cookieManager.removeAllCookie();
		webView = (WebView) findViewById(R.id.webView);
		webView.getSettings().setSavePassword(false);
		webView.getSettings().setSaveFormData(false);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setSupportZoom(true);
		webView.getSettings().setBuiltInZoomControls(true);
		progressBar = (ProgressBar)findViewById(R.id.network);
		
		webView.setWebChromeClient(new WebChromeClient(){
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				super.onProgressChanged(view, newProgress);
				progressBar.setProgress(newProgress);
			}
		});
		
		webView.setWebViewClient(new WebViewClient(){
			
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				progressBar.setVisibility(View.GONE);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				progressBar.setVisibility(View.VISIBLE);
				progressBar.setProgress(0);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if(url.startsWith(ApiConstant.URL_ACTIVITY_CALLBACK)){
					Intent intent = new Intent();
					intent.setData(Uri.parse(url));
					setResult(2, intent);
					WebViewActivity.this.finish();
				}
				view.loadUrl(url);  
	            return true;  
			}
		});
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
    
}